package com.zimin.web.exception;

import com.alibaba.fastjson.JSON;
import com.zimin.common.enums.Number;
import com.zimin.common.exception.InjectException;
import com.zimin.shared.enums.ResponseStatusEnum;
import com.zimin.shared.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zimin
 * @date : 2021/6/24 3:34 下午
 * @function :
 */
@RestControllerAdvice
@Slf4j
public class GraceExceptionHandler {

    /**
     * 错误信息的构建工具.
     */
    @Autowired
    private ErrorInfoBuilder errorInfoBuilder;

    /**
     * @param e
     * @author: zimin
     * @description: TODO:  自定义空指针异常的返回结果
     * @since: 2021-06-24 15:50:33
     * @return: com.zimin.common.response.Result
     */
    @ExceptionHandler(NullPointerException.class)
    public Result returnNullPointerException(HttpServletRequest request, NullPointerException e) {
        var errorInfo = errorInfoBuilder.getErrorInfo(request, e);
        e.printStackTrace();
        log.info(errorInfo.toString());
        return Result.exception(ResponseStatusEnum.NULL_POINT);
    }

    /**
     * @param e
     * @author: zimin
     * @description: TODO: 自定义异常的返回结果
     * @since: 2021-06-24 15:53:49
     * @return: com.zimin.common.response.Result
     */
    @ExceptionHandler(InjectException.class)
    public Result returnInjectException(HttpServletRequest request, InjectException e) {
        return Result.exception(e.getResponseStatusEnum());
    }

    /**
     * @param e
     * @author: zimin
     * @description: TODO: 上传文件超过最大限制
     * @since: 2021-06-24 15:57:24
     * @return: com.zimin.common.response.Result
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result returnMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        e.printStackTrace();
        return Result.exception(ResponseStatusEnum.FILE_MAX_SIZE_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result returnMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        var bindingResult = e.getBindingResult();
        var errors = getErrorsList(bindingResult);
        return Result.errorMsg(JSON.toJSONString(errors));
    }

    /**
     * 参数校验
     *
     * @param result
     * @return
     */
    public Map<String, String> getErrors(BindingResult result) {
        var map = new HashMap<String, String>(Number.TEN.getIntNum());
        var errorList = result.getFieldErrors();
        for (FieldError error : errorList) {
            // 发送验证错误的时候所对应的某个属性
            var field = error.getField();
            // 验证的错误消息
            var msg = error.getDefaultMessage();
            map.put(field, msg);
        }
        return map;
    }

    public List<String> getErrorsList(BindingResult result) {
        var msgList = new ArrayList<String>();
        var errorList = result.getFieldErrors();
        for (FieldError error : errorList) {
            // 发送验证错误的时候所对应的某个属性
            // 验证的错误消息
            String msg = error.getDefaultMessage();
            msgList.add(msg);
        }
        return msgList;
    }
}
